<?php

class Admin_projectController extends Admin_baseController{
    public function listAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);

        $title = Request::getParams('name');

        $where = $value = $url = array();
        if ($title) {
            $where[] = 'name like ?';
            $value[] = '%' . $title . '%';
            $url['name'] = $title;
        }

        $total = self::model('project_list')->count(null, $where, $value);
        $list = self::model('project_list')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);

        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'total' => $total,
            'list' => $list,
            'name' => $title,
            'categoryList' => self::model('project')->getListTree(),
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_project/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ));
        $this->display();
    }

    public function editAction(){
        $data = [];
        $project_id=Request::getParams("projectId");
        $data['name'] = Request::getParams('name');
        $data['images'] = Request::getParams('image');
        $data['categoryId']=Request::getParams('categoryId');
        $data['address']=Request::getParams('address');
        $data['project_time']=Request::getParams('project_time');
        $data['prodect_desc']=Request::getParams('prodect_desc');
        $data['start_time']=Request::getParams('start_time');
        $data['end_time']=Request::getParams('end_time');
        $data['sort']=Request::getParams('sort');
        $data['questionnaire']=Request::getParams('questionnaire');

        if($project_id&&!$info = self::model('project_list')->getOne('id=?', $project_id)){
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if(Request::isGet()){
            $this->assign([
                'info'=>$info,
                'categoryList' => self::model('project')->getListTree(),
            ]);

            $this->display();
            exit();
        }

        if(empty($data['images'] )){
            Response::showMsg('请上传图片');
        }
        if(!empty($data['categoryId'] )){
            $categrol=self::model("project")->getOne("id=?",$data['categoryId']);
            $data["categral2"]=$data['categoryId'];
            $data["categral1"]=$categrol["parent_id"]==0?$data['categoryId']:$categrol["parent_id"];

            unset($data["categoryId"]);
        }

        if(!empty($data['start_time'])){
            $data['start_time']=strtotime($data['start_time']);
        }
        if(!empty($data['end_time'])){
            $data['end_time']=strtotime($data['end_time']);
        }
        if(!empty($data['start_time'])&&!empty($data['end_time'])){
            if($data['start_time']>$data['end_time']){
                Response::showMsg('请输入结束时间不能小于开始时间');
            }
        }

        $data["update_time"]=date("Y-m-d H:i:s",time());
        if($project_id){
            self::model("project_list")->update($data,"id=?",$project_id);
            Response::ajax(200, '修改成功');
        }else{
            $data["add_time"]=date("Y-m-d H:i:s",time());
            self::model("project_list")->insert($data);
            Response::ajax(200, '添加成功');
        }

    }

    public function categoryAction(){
        $this->assign(array(
            'list' => self::model('project')->getListTreeAll(),
        ));
        $this->display();
    }
}